package com.sunseaypw.base;
import java.io.PrintWriter;
import java.io.StringWriter;
public final class BizException extends Exception{
    private String friendMsg = "";
    public BizException(String friendMsg,String detailMsg){  
        super(detailMsg);     
        this.friendMsg=friendMsg;
    }
    public BizException(String friendMsg,String detailMsg,Throwable cause){        
        super(detailMsg,cause);
        this.friendMsg=friendMsg;
    }
    public static void outExptionLog(Throwable te){
        outExptionLog(te,"");
    }
    public static void outExptionLog(Throwable te,String bizCatalog){
        var epi = GetExceptionInfo(te);
        var all = new String[]{epi.getFriendMsg(),epi.getDetailMsg(),epi.getStackTrace()};        
        MyLogger.error(String.join(" ",all),bizCatalog,te);
    }
    public static ExceptionInfo GetExceptionInfo(Throwable te){
        var epi=new ExceptionInfo();           
        var sbFMsgInfo=new StringBuilder();
        var sbDMsgInfo=new StringBuilder();
        var cause=te;
        while(cause!=null){
            if((cause instanceof BizException) && !cause.getMessage().isEmpty()){
                sbFMsgInfo.append(((BizException)cause).friendMsg +"  ");
            }
            sbDMsgInfo.append(cause.toString()+"  ");
            cause=cause.getCause();
        }
        epi.setFriendMsg(sbFMsgInfo.toString());
        //System.out.println(epi.getFriendMsg());   
        epi.setDetailMsg(sbDMsgInfo.toString());
        //System.out.println(epi.getDetailMsg());                               
        var swTraceInfo = new StringWriter();
        te.printStackTrace(new PrintWriter(swTraceInfo));
        epi.setStackTraceAll(swTraceInfo.toString());
        //System.out.println(epi.getStackTrace());          
        return epi;
    }
}